package com.gmall.data.utils;

import com.gmall.data.common.config.Config;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Slf4j
public class JedisPoolUtil {

    private static volatile JedisPool jedisPool = null;

    public static JedisPool getJedisPoolInstance() {
        if (jedisPool == null) {
            synchronized (JedisPoolUtil.class) {
                if (jedisPool == null) {
                    try {
                        JedisPoolConfig poolConfig = new JedisPoolConfig();
                        poolConfig.setMaxTotal(10);
                        poolConfig.setMaxIdle(2);
                        poolConfig.setMinIdle(1);
                        poolConfig.setMaxWaitMillis(10000);

                        poolConfig.setTestOnBorrow(true); // 获得jedis实例时检查可用性(ping): 如果true,那么得到的jedis实例均可用

                        jedisPool = new JedisPool(poolConfig, Config.redisHost(), Config.redisPort(),
                                10000, Config.redisPassword(), Config.redisDB());
                    } catch (Exception e) {
                        log.error("JedisPoolUtil.getJedisPoolInstance error,{}", e);
                    }
                }
            }
        }
        return jedisPool;
    }

}
